import { doLoginApi } from '@/api/loginApi'
import { setStoreUser } from '@/utils/store'

export default {
  namespaced: true,
  state: {
    uid: 0,
    nickname: '',
    token: ''
  },
  mutations: {
    setUser(state, data) {
      state.uid = data.uid
      state.nickname = data.nickname
      state.token = data.token
    },
    // 用户登录数据持久化操作
    setStore(state, data) {
      setStoreUser(data)
    }
  },
  actions: {
    // 完成用户的登录
    async setUserAction({ commit }, data) {
      try {
        let ret = await doLoginApi(data)
        if (ret.code == 0) {
          commit('setUser', ret.data)
          commit('setStore', ret.data)
          return true
        } else {
          return false
        }
      } catch (error) {
        return false
      }
    }
  }
}
